package handler

import (
	"encoding/json"
	"fmt"
	"github.com/gocolly/colly"
	"sbd_zgqx/models"
	"sbd_zgqx/service"
	"sbd_zgqx/utils"
	"strings"
)

type ProvinceHandler struct {
	ProvinceSrv service.ProvinceService
}

type Result struct {
	Msg  string `json:"msg"`
	Code int    `json:"code"`
	Data string `json:"data"`
}

func (handler ProvinceHandler) ReListHandler() {
	var isPro models.Province
	var result Result
	// isBreeder := models.BreederNo{}

	urlProvince := fmt.Sprintf("https://weather.cma.cn/api/dict/province")
	c := colly.NewCollector(colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"))

	c.OnRequest(func(r *colly.Request) {
		// r.Headers.Set("Cookie", "JSESSIONID=A87C2BE548AF94B7CC4B9FA8E40178D0; pageSize=20; pageNo=1")
		// r.Headers.Set("Content-Type", " application/x-www-form-urlencoded")

	})
	c.OnResponse(func(r *colly.Response) {
		err := json.Unmarshal(r.Body, &result)
		// fmt.Println(string(r.Body))
		if err != nil {
			fmt.Println("aaaa ====>", err)
		}
		// fmt.Println(result.Data)

		as := strings.Split(result.Data, "|")
		node := &utils.SnowFlake{}
		_ = node.Init(1, 5)
		for _, v := range as {
			id, _ := node.NextId()
			area := strings.Split(v, ",")
			isPro.ID = id
			isPro.CID = 000000
			isPro.Serial = utils.ReplaceStr(area[0])
			isPro.Area = utils.ReplaceStr(area[1])

			_, _ = handler.ProvinceSrv.InsterProvinceSrv(isPro)

			handler.ReSerialInfoHandler(isPro.Serial, id)
		}
		// fmt.Println(as[0])
		// for _, v := range result.Rows {
		// 	v.ID = utils.SnowId()
		// 	v, err = ReListInfoHandler(v)
		// 	if err != nil {
		// 		fmt.Println("bbbb ====>", err)
		// 	}
		// 	isBreeders = append(isBreeders, v)
		// }

	})
	c.Visit(urlProvince)
	return
}

func (handler ProvinceHandler) ReSerialInfoHandler(serial string, cid int64) {
	var isPro models.Province
	var result Result
	// isBreeder := models.BreederNo{}

	urlProvince := fmt.Sprintf("https://weather.cma.cn/api/dict/province/%s", serial)
	c := colly.NewCollector(colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"))

	c.OnRequest(func(r *colly.Request) {
		// r.Headers.Set("Cookie", "JSESSIONID=A87C2BE548AF94B7CC4B9FA8E40178D0; pageSize=20; pageNo=1")
		// r.Headers.Set("Content-Type", " application/x-www-form-urlencoded")

	})
	c.OnResponse(func(r *colly.Response) {
		err := json.Unmarshal(r.Body, &result)
		// fmt.Println(string(r.Body))
		if err != nil {
			fmt.Println("bbbb ====>", err)
		}
		fmt.Println(result.Data)
		as := strings.Split(result.Data, "|")
		node := &utils.SnowFlake{}
		_ = node.Init(1, 5)
		for _, v := range as {
			id, _ := node.NextId()
			area := strings.Split(v, ",")
			isPro.ID = id
			isPro.CID = cid
			isPro.Serial = utils.ReplaceStr(area[0])
			isPro.Area = utils.ReplaceStr(area[1])

			_, _ = handler.ProvinceSrv.InsterProvinceSrv(isPro)
		}
		// node := &utils.SnowFlake{}
		// _ = node.Init(1, 5)
		// for _, v := range as {
		// 	id, _ := node.NextId()
		// 	area := strings.Split(v, ",")
		// 	isPro.ID = id
		// 	isPro.CID = 000000
		// 	isPro.Serial = utils.ReplaceStr(area[0])
		// 	isPro.Area = utils.ReplaceStr(area[1])
		//
		// 	is, err := handler.ProvinceSrv.InsterProvinceSrv(isPro)
		// 	if err != nil {
		// 		fmt.Println(is, err)
		// 	}
		// }

	})
	c.Visit(urlProvince)
	return
}
